const Main=require('yg-Main');
const Data=require('yg-Data');
const GameServerManager=require('yg-GameServerManager');
cc.Class({
    extends: require('yg-Component'),

    properties: {
        loaddingNode:cc.Node,
        mainNode:cc.Node,
        progressBar:cc.ProgressBar,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        Object.setPrototypeOf(Main, this);
    },

    start () {
        Main.gameLoadding();
        //Main.progressBar.progress=1;
    },
    //加载完后显示指定的prefab实例作为场景
    defaultLoadding(){
        cc.resources.loadDir('UI',cc.Prefab,(finish,total,item)=>{
            Main.Loadding.progressBar.progress=finish/total;
        },()=>{
            //判断是否本地存储有登录信息，如果有，则自动登录
            if(Data.debug){
                this.lobbyLoadding();
                return;
            }
            Data.init();
            Data.request(()=>{
                if(Data.status===0){
                    cc.resources.load('login/index', cc.Prefab, (err, prefab) => {
                        this.mainNode.addChild(cc.instantiate(prefab));
                        this.loaddingNode.active=false;
                    });
                }else{
                    this.lobbyLoadding();
                }
            });
        });
    },
    //加载大厅场景
    lobbyLoadding(){
        console.log('开始加载大厅资源')
        GameServerManager.init();
        this.loaddingNode.active=true;
        cc.resources.loadDir('lobby',cc.Prefab,(finish,total,item)=>{
            Main.Loadding.progressBar.progress=finish/total;
        },()=>{
            cc.resources.load('lobby/index', cc.Prefab, (err, prefab) => {
                this.mainNode.destroyAllChildren();
                this.mainNode.addChild(cc.instantiate(prefab));
                this.loaddingNode.active=false;
            });
        });
    },
    //加载游戏场景
    gameLoadding(){
        console.log('开始加载游戏资源')
        this.loaddingNode.active=true;
        cc.resources.loadDir('game',cc.Prefab,(finish,total,item)=>{
            Main.Loadding.progressBar.progress=finish/total;
        },()=>{
            cc.resources.load('game/index', cc.Prefab, (err, prefab) => {
                this.mainNode.destroyAllChildren();
                this.mainNode.addChild(cc.instantiate(prefab));
                this.loaddingNode.active=false;
            });
        });
    },
    //加载房间场景
    roomLoadding(){
        console.log('开始加载房间资源')
        this.loaddingNode.active=true;
        cc.resources.loadDir('room',cc.Prefab,(finish,total,item)=>{
            Main.Loadding.progressBar.progress=finish/total;
        },()=>{
            cc.resources.load('room/index', cc.Prefab, (err, prefab) => {
                this.mainNode.destroyAllChildren();
                this.mainNode.addChild(cc.instantiate(prefab));
                this.loaddingNode.active=false;
            });
        });
    }
});
